﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneralLibs;
using CustomSecurity;
using Vouchers;
using Lists;
using BasicForms;

namespace QuanLyKho
{
    public partial class FrmChonPhieuNhapXuatNhanhChoCacNghiepVuDacBiet : BasicForms.FrmOKCancel
    {
        protected DieuKienTimKiemChungTu _DieuKienTimKiem = new DieuKienTimKiemChungTu();
        protected int _ItemsPerPage = 100;
        protected int _PageNumber = 1;
        protected int _NumOfPages = 0;
        protected Right _Right = CustomSecurity.Right.None;
        protected DanhSachChungTu _DanhSachChungTu = new DanhSachChungTu();

        public ChungTu SelectedChungTu = null;

        public FrmChonPhieuNhapXuatNhanhChoCacNghiepVuDacBiet(string maLoaiChungTu)
        {
            InitializeComponent();
            _DieuKienTimKiem.LoaiChungTu = DanhMucLoaiChungTu.Instance.GetByMa(maLoaiChungTu);
        }

        private void FrmChonPhieuNhapChoPhieuChuyenKho_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;

            try
            {
                _DieuKienTimKiem.TuNgay = new NullableDate(new DateTime(CommonLib.GetSystemDateTime().Date.Year, 1, 1, 00,00, 01));
                _DieuKienTimKiem.DenNgay = new NullableDate(new DateTime(CommonLib.GetSystemDateTime().Date.Year, 12, 31,23,59, 59));
                _DieuKienTimKiem.OrderBy = "Ngay";
                _DieuKienTimKiem.ASC = false;

                _Right = User.CurUser.GetRight(Name);
                LoadData();
                FrmBase.FormatNumberInGridView(gridViewDsChungTu);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public virtual void LoadData()
        {
            _DanhSachChungTu.LoadFromDB(_DieuKienTimKiem.BuildSqlStatement(_ItemsPerPage, _PageNumber));
            gridControlDsChungTu.DataSource = _DanhSachChungTu.DataTable;


            rItemLookUpEditDoiTuongNghiepVu.DataSource = DanhMucDoiTuongNghiepVu.Instance.DataView;
            rItemGridLookUpEditTienTe.DataSource = DanhMucTienTe.Instance.DataView;
            rItemGridLookUpEditKho1.DataSource = DanhMucKho.Instance.DataView;
            rItemGridLookUpEditKho2.DataSource = DanhMucKho.Instance.DataView;
            rItemGridLookUpEditNguoiTaoPhieu.DataSource = UserList.Instance.DataTable.DefaultView;

            gridViewDsChungTu.Columns["DoiTuongNghiepVu1ID"].ColumnEdit = rItemLookUpEditDoiTuongNghiepVu;
            gridViewDsChungTu.Columns["TienTeID"].ColumnEdit = rItemGridLookUpEditTienTe;
            gridViewDsChungTu.Columns["Kho1ID"].ColumnEdit = rItemGridLookUpEditKho1;
            gridViewDsChungTu.Columns["Kho2ID"].ColumnEdit = rItemGridLookUpEditKho2;
            gridViewDsChungTu.Columns["UserID"].ColumnEdit = rItemGridLookUpEditNguoiTaoPhieu;

            rItemGridLookUpEditKho1.DisplayMember = "Ten";
            rItemGridLookUpEditKho1.ValueMember = "ID";
            rItemGridLookUpEditKho2.DisplayMember = "Ten";
            rItemGridLookUpEditKho2.ValueMember = "ID";
            rItemGridLookUpEditNguoiTaoPhieu.DisplayMember = "UserName";
            rItemGridLookUpEditNguoiTaoPhieu.ValueMember = "ID";

            gridViewDsChungTu.Columns["Ngay"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
            DataView dv = GeneralLibs.Config.GiaoDienFormDanhSachChungTu.Filter(_DieuKienTimKiem.LoaiChungTu.ID);
            foreach (DataRowView row in dv)
            {
                string columnName = (string)row["Column Name"];
                gridViewDsChungTu.Columns[columnName].Visible = (bool)row["HienThi"];
                gridViewDsChungTu.Columns[columnName].Caption = (string)row["TenHienThi"];
                gridViewDsChungTu.Columns[columnName].Width = (int)row["DoRong"];
                gridViewDsChungTu.Columns[columnName].SortIndex = (int)row["ThuTuHienThi"];
                if (columnName == "Ngay")
                {
                    gridViewDsChungTu.Columns[columnName].DisplayFormat.FormatString = "dd/mm/yyyy hh:mm";
                }
            }
        }

        private void btnOK_Click_1(object sender, EventArgs e)
        {
            ChoosedRows();
        }

        private void ChoosedRows()
        {
            foreach (int index in gridViewDsChungTu.GetSelectedRows())
            {
                SelectedChungTu = _DanhSachChungTu.GetByDataRow(gridViewDsChungTu.GetDataRow(index));
                return;
            }
        }

        private void gridControlDsChungTu_DoubleClick(object sender, EventArgs e)
        {
            ChoosedRows();
            this.DialogResult = DialogResult.OK;
        }
    }
}
